
#!/bin/bash

# begin --- To change
base_path="/home/zihan/arkcompiler"
out_path="runtime_core/static_core/out"
// 更改为需要执行code check的仓库
test_path="${base_path}/ets_frontend"
# end --- To change


clang_format_path="runtime_core/static_core/scripts/code_style/run_code_style_tools.sh"
clang_tidy_path="runtime_core/static_core/scripts/clang-tidy/clang_tidy_check.py"
static_core_path="runtime_core/static_core"
cd ${test_path}

# 从git status中获取修改的文件
modified_files=$(git status --porcelain | awk '/^[ M|A]/ {print $2}')

if [ $# -ge 1 ]; then
    if [ "$1" == "log" ]; then
        # 从 第一个git log中获取修改的文件
        latest_commit=$(git log -1 --pretty=format:"%H")
        modified_files=$(git diff-tree --no-commit-id --name-only -r $latest_commit)
    fi
fi

# 跑 clang-format
for file in $modified_files; do
    if [[ $file == *".cpp" || $file == *".h" ]]; then
        run_clang_format="bash ${base_path}/${clang_format_path} ${file}"
        echo ${run_clang_format}
        eval ${run_clang_format}
    fi
done

# 跑 clang-tidy
filename_filter=""
for file in $modified_files; do
    if [[ $file == *".cpp" || $file == *".h" ]]; then
        if [[ -n $filename_filter ]]; then
            filename_filter+="|"
        fi
        filename_filter+="${file}"
    fi
done

if [[ -n $filename_filter ]]; then
    run_clang_tidy="python ${base_path}/${clang_tidy_path} ${base_path}/${static_core_path} ${base_path}/${out_path} --filename-filter '${filename_filter}'"
    echo ${run_clang_tidy}
    eval ${run_clang_tidy}
fi